package com.app.piemoment.entity;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

public class OppositeInfo implements Parcelable{
	private long puid;
	private String nickname;
	private int sex;//0=false 1=true
	private int height;
	private int weight;
	private String birthday;
	private String degree;
	private String school;
	private String occupation;// 职业
	private String domain;
	private Address birthPlace;
	private Address currentPlace;
	private ArrayList<Photo> photos;
	
	public OppositeInfo() {
		super();
	}

	OppositeInfo(Parcel in) {
		this.puid = in.readLong();
		this.nickname = in.readString();
		this.sex = in.readInt();
		this.height = in.readInt();
		this.weight = in.readInt();
		this.birthday = in.readString();
		this.degree = in.readString();
		this.school = in.readString();
		this.occupation = in.readString();
		this.domain = in.readString();
		this.birthPlace = in.readParcelable(Address.class.getClassLoader());
		this.currentPlace = in.readParcelable(Address.class.getClassLoader());
		
		photos = new ArrayList<Photo>();
    	int listSize = in.readInt();
		for (int i = 0; i < listSize; i++) {
			Photo photo = in.readParcelable(Photo.class.getClassLoader());
			photos.add(photo);
		}
	}

	public static final Parcelable.Creator<OppositeInfo> CREATOR = new Creator<OppositeInfo>() {
		@Override
		public OppositeInfo[] newArray(int size) {
			return new OppositeInfo[size];
		}

		@Override
		public OppositeInfo createFromParcel(Parcel source) {
			return new OppositeInfo(source);
		}
	};

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeLong(puid);
		dest.writeString(nickname);
		dest.writeInt(sex);
		dest.writeInt(height);
		dest.writeInt(weight);
		dest.writeString(birthday);
		dest.writeString(degree);
		dest.writeString(school);
		dest.writeString(occupation);
		dest.writeString(domain);
		dest.writeParcelable(birthPlace, flags);
		dest.writeParcelable(currentPlace, flags);
		if (photos != null) {
			dest.writeInt(photos.size());
            for (int i = 0; i < photos.size(); i++) {
            	dest.writeParcelable(photos.get(i), flags);
            }
		} else {
			dest.writeInt(0);
		}
	}
	

	public long getPuid() {
		return puid;
	}

	public void setPuid(long puid) {
		this.puid = puid;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public int getSex() {
		return sex;
	}

	public void setSex(int sex) {
		this.sex = sex;
	}

	public String getBirthday() {
		return birthday;
	}

	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getWeight() {
		return weight;
	}

	public void setWeight(int weight) {
		this.weight = weight;
	}

	public String getDegree() {
		return degree;
	}

	public void setDegree(String degree) {
		this.degree = degree;
	}

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}

	public String getOccupation() {
		return occupation;
	}

	public void setOccupation(String occupation) {
		this.occupation = occupation;
	}

	public ArrayList<Photo> getPhotos() {
		return photos;
	}

	public void setPhotos(ArrayList<Photo> photos) {
		this.photos = photos;
	}

	public Address getBirthPlace() {
		return birthPlace;
	}

	public void setBirthPlace(Address birthPlace) {
		this.birthPlace = birthPlace;
	}

	public Address getCurrentPlace() {
		return currentPlace;
	}

	public void setCurrentPlace(Address currentPlace) {
		this.currentPlace = currentPlace;
	}
	

	public String getDomain() {
		return domain;
	}

	public void setDomain(String domain) {
		this.domain = domain;
	}

	public void parser(JSONObject jsonEntity) throws JSONException {
		if(jsonEntity!=null) {
			if(jsonEntity.has("puid")) {
				setPuid(jsonEntity.getLong("puid"));
			}
			if(jsonEntity.has("nickname")) {
				setNickname(jsonEntity.getString("nickname"));
			}
			if(jsonEntity.has("sex")) {
				setSex(jsonEntity.getBoolean("sex")?1:0);
			}
			if(jsonEntity.has("birthday")) {
				setBirthday(jsonEntity.getString("birthday"));
			}
			if(jsonEntity.has("height")) {
				setHeight(jsonEntity.getInt("height"));
			}
			if(jsonEntity.has("weight")) {
				setWeight(jsonEntity.getInt("weight"));
			}
			if(jsonEntity.has("degree")) {
				setDegree(jsonEntity.getString("degree"));
			}
			if(jsonEntity.has("school")) {
				setSchool(jsonEntity.getString("school"));
			}
			if(jsonEntity.has("occupation")) {
				setOccupation(jsonEntity.getString("occupation"));
			}
			if(jsonEntity.has("domain")) {
				setDomain(jsonEntity.getString("domain"));
			}
			if(jsonEntity.has("birth_place")) {
				Address bithPlace = new Address();
				bithPlace.parser(jsonEntity.getJSONObject("birth_place"));
				setBirthPlace(bithPlace);
			}
			if(jsonEntity.has("current_location")) {
				Address currentPlace = new Address();
				currentPlace.parser(jsonEntity.getJSONObject("birth_place"));
				setCurrentPlace(currentPlace);
			}
			if(jsonEntity.has("photos")) {
				JSONArray jsonArray = jsonEntity.getJSONArray("photos");
				if(jsonArray!=null&&jsonArray.length()>0) {
					ArrayList<Photo> list = new ArrayList<Photo>();
					Photo photo = null;
					for(int i=0;i<jsonArray.length();i++) {
						photo = new Photo();
						photo.setFilepath((String) jsonArray.opt(i));
						list.add(photo);
					}
					setPhotos(list);
				}
				
			}
		}
	}


}
